package com.dongway.netcondition.web;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dongway.core.common.Paginator;
import com.dongway.core.web.AbstractBaseController;
import com.dongway.netcondition.dto.AutoCompleteItem;
import com.dongway.netcondition.dto.DevStateDTO;
import com.dongway.netcondition.service.DevNetStateService;
import com.dongway.netcondition.service.DevSelectHelper;

@Controller
public class StationController extends AbstractBaseController {

    @Autowired
    private DevNetStateService devStateService;
    @Autowired
    private DevSelectHelper devSelectHelper;

    
    @RequestMapping("station.htm")
    public String station(ModelMap modelMap) {
        List<AutoCompleteItem> branches = devSelectHelper.getAllBranch();
        modelMap.put("branches", branches);
        return "net/station";
    }

    @RequestMapping("/station/station.json")
    public String home(Integer branchId, Paginator paginator, ModelMap modelMap) {

        List<DevStateDTO> list;
        if (branchId == null || branchId == 0) {
            list = devStateService.all(null);
        } else {
            list = devStateService.getDevStatesByBranchId(branchId, null);
        }
        modelMap.put("branchId", branchId);
        // 定义设备延迟，专网不通，VPN不通，双网不通 的数量
        int pn = 0;
        int pndelay = 0;

        int vpn = 0;
        int vpndelay = 0;

        int vpndev = 0;
        int vpndevdelay = 0;

        int dpn = 0;
        List<Integer> listpn = new ArrayList<Integer>();
        List<Integer> listdelay = new ArrayList<Integer>();
        modelMap.put("devNetStates", list);

        for (int i = 0; i < list.size(); i++) {

            if (list.get(i).getState() == 0
                    && list.get(i).getToPnServerState() == 2
                    && list.get(i).getToPnLocalState() == 2) {
                pn++;
            }

            if (list.get(i).getState() == 0
                    && (list.get(i).getToPnServerState() != 0 && list.get(i)
                        .getToPnLocalState() != 0)
                    && (list.get(i).getToPnServerState() == 1 || list.get(i)
                        .getToPnLocalState() == 1)) {
                pndelay++;
            }

            if (list.get(i).getState() == 0
                    && list.get(i).getToVpnServerState() == 2) {
                vpn++;
            } else if (list.get(i).getState() == 0
                    && list.get(i).getToVpnServerState() == 1) {
                vpndelay++;
            }

            if (list.get(i).getState() == 0
                    && list.get(i).getToVpnLocalState() == 2) {
                vpndev++;
            } else if (list.get(i).getState() == 0
                    && list.get(i).getToVpnLocalState() == 1) {
                vpndevdelay++;
            }

            if (list.get(i).getState() == 0
                    && list.get(i).getToPnServerState() == 2
                    && list.get(i).getToPnLocalState() == 2
                    && list.get(i).getToVpnServerState() == 2) {
                dpn++;
            }

        }
        listpn.add(pn);
        listpn.add(vpn);
        listpn.add(vpndev);

        listdelay.add(pndelay);
        listdelay.add(vpndelay);
        listdelay.add(vpndevdelay);

        modelMap.put("pn", listpn);
        modelMap.put("pndelay", listdelay);
        modelMap.put("dpn", dpn);

        return "net/station/main";
    }

}
